package com.sk.myview;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.ByteArrayEntity;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.params.CoreConnectionPNames;
import org.apache.http.protocol.HTTP;
import org.apache.http.util.EntityUtils;
import org.json.JSONObject;
import android.util.Log;

public class HTTPHelper {
	
	private static int CONNECT_TIME_OUT = 15 * 1000;
    private static int READ_TIME_OUT = 90 * 1000; 
    public static final String URL_WSUSER = "http://192.168.200.50/patrol/WSUser.asmx";
    public static final String URL_WSPatrol = "http://192.168.200.50/patrol/WSPatrol.asmx";
    public static final String URL_WSTest = "http://192.168.200.50/patrol/Test.asmx";
    
	public static String HTTPClientPost(String url,String key,JSONObject params){
		String result = "";
		HttpPost request = new HttpPost(url);
		request.addHeader(HTTP.CONTENT_TYPE, "application/json; charset=utf-8");
		request.getParams().setParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, CONNECT_TIME_OUT);
		request.getParams().setParameter(CoreConnectionPNames.SO_TIMEOUT, READ_TIME_OUT);
		try {
			if(params!=null){
				HttpEntity paramEntity = new StringEntity("aaa=aaaaaaa",HTTP.UTF_8);
				request.setEntity(paramEntity);				
			}
			HttpResponse response =  new DefaultHttpClient().execute(request);
			if(response.getStatusLine().getStatusCode()==200){
				result = EntityUtils.toString(response.getEntity());
			}else if (response.getStatusLine().getStatusCode()==500) {
				result="false";          
			}else if(response.getStatusLine().getStatusCode()==404) {
				result="false";
			}
		} catch (ClientProtocolException e) {			
			e.printStackTrace();
			result="false";              
		} catch (IOException e) {
			e.printStackTrace();
			result="false";
		}
		return result;
	}
	public static String HTTPClientGet(String tableName){
		String result = "";
		String target = "http://192.168.200.21/easypatrol/Data/DataTransfer.aspx/getDataTabe?tableName="+tableName;
		HttpGet request = new HttpGet(target);
//		request.addHeader(HTTP.CONTENT_TYPE, "application/json; charset=utf-8");
		request.getParams().setParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, CONNECT_TIME_OUT);
		request.getParams().setParameter(CoreConnectionPNames.SO_TIMEOUT, READ_TIME_OUT);
		try {
			HttpResponse response = new DefaultHttpClient().execute(request);
			if(response.getStatusLine().getStatusCode()==200){
				result = EntityUtils.toString(response.getEntity());
			}else{
				result = "false";
			}
		} catch (ClientProtocolException e) {
			result = "false";
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		Log.v(tableName, result);
		return result;
	}
	public static String UrlConnectionPost(String target,String key,JSONObject params){
		String result = "";
		URL url = null;
		HttpURLConnection conn = null;
		InputStream is = null;
		OutputStream os = null;
		try {	
			byte[] data = (key+"="+params.toString()).getBytes(HTTP.UTF_8);
			url = new URL(target);
			conn = (HttpURLConnection)url.openConnection();
			conn.setRequestMethod("POST");
			conn.setDoOutput(true);
			conn.setDoInput(true);
			conn.setUseCaches(false);
			conn.setInstanceFollowRedirects(false);			
			conn.setChunkedStreamingMode(10240);
//			conn.setRequestProperty("Connection", "Keep-Alive");
//			conn.setRequestProperty(HTTP.CONTENT_TYPE, "text/plain; charset=utf-8");
			conn.setRequestProperty("Content-type", "application/x-www-form-urlencoded; charset=utf-8");
			os = conn.getOutputStream();
			byte [] temp = new byte[10240];
			int i =0;
			ByteArrayInputStream bais = new ByteArrayInputStream(data);
			while ((i=bais.read(temp))!=-1) {
				os.write(temp,0,i);
			}
			
			os.flush();
			os.close();
			conn.connect();
			is = conn.getInputStream();
			byte [] temOut = new byte[is.available()];
			ByteArrayOutputStream baos = new ByteArrayOutputStream();
			while ((i=is.read(temOut))!=-1) {
				baos.write(temOut, 0, i);
			}
			result = new String(baos.toByteArray(),HTTP.UTF_8);
			
		} catch (MalformedURLException e) {
			e.printStackTrace();
			result = "false";
		} catch (IOException e) {
			e.printStackTrace();
			result = "false";
		}finally{
			if(is!=null){
				try {
					is.close();
				} catch (IOException e) {
					e.printStackTrace();
				}				
			}
			conn.disconnect();
		}
		return result.toString();
	}
	public static String UrlConnectionGet(String target){
		String result = "";
		URL url  = null;
		HttpURLConnection conn = null;
		InputStream is = null;
		try {
			url = new URL(target);
			conn = (HttpURLConnection)url.openConnection();
			conn.setUseCaches(false);
			conn.setInstanceFollowRedirects(false);
//			conn.setRequestProperty(HTTP.CONTENT_TYPE, "application/json; charset=utf-8");
			conn.connect();
			is = conn.getInputStream();
			byte [] temp = new byte [8192];
			ByteArrayOutputStream baos = new ByteArrayOutputStream();
			while (is.read(temp)!=-1) {
				baos.write(temp);
			}
			baos.flush();
			baos.close();
			result = new String(baos.toByteArray(),HTTP.UTF_8);			
		} catch (MalformedURLException e) {
			e.printStackTrace();
			result = "false";
		} catch (IOException e) {
			e.printStackTrace();
			result = "false";
		}finally{
			if(is!=null){
				try {
					is.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
				conn.disconnect();
			}
		}
		return result.toString();
	}
}
